<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.appspot.gaeforum309.db.*" %>
<%@ page import="com.appspot.gaeforum309.core.ForumAPI %>
<%@ page import="java.util.*" %>
<!-- <%=request.getParameter("action") %> -->
<%

	if("createCat".equals(request.getParameter("action")))
	{
		ForumAPI.createCategory(request.getParameter("name"), request.getParameter("descr"));
		response.sendRedirect(request.getRequestURI());
		ForumAPI.closeDB();
		return;
	}
	if("createTopic".equals(request.getParameter("action")))	
	{
		DBCategory dbcategory = ForumAPI.getCategory(request.getParameter("cat"));
		if(dbcategory == null)
		{
			ForumAPI.closeDB();
			return;
		}
		
		ForumAPI.createTopic(dbcategory, request.getParameter("name"), request.getParameter("descr"));
		response.sendRedirect(request.getRequestURI());
		ForumAPI.closeDB();
		return;
	}
	if("acceptCon".equals(request.getParameter("action")))
	{
		DBUser dbuser = ForumAPI.userLogin();
		
		dbuser.userWelcome();
		response.sendRedirect(request.getRequestURI());
		ForumAPI.closeDB();
		return;
	}
	if("creatConversation".equals(request.getParameter("action")))
	{
		DBTopic dbtopic = ForumAPI.getTopic(request.getParameter("top"));
		if(dbtopic == null)
		{
			ForumAPI.closeDB();
			return;
		}
		ForumAPI.createConversation(
				dbtopic, 
				request.getParameter("title"), 
				request.getParameter("descr")
			);
		response.sendRedirect(request.getRequestURI());
		ForumAPI.closeDB();
		return;
	}
	if("creatComment".equals(request.getParameter("action")))
	{
		DBConversation dbconv = ForumAPI.getConversation(request.getParameter("conv"));
		DBUser dbuser = ForumAPI.userLogin();
		
		if(dbconv == null || dbuser == null)
		{
			response.sendRedirect(request.getRequestURI());
			ForumAPI.closeDB();
			return;
		}
		ForumAPI.createComment(
				dbconv, 
				request.getParameter("message"),
				dbuser
			);
		response.sendRedirect(request.getRequestURI());
		ForumAPI.closeDB();
		return;
	}
	if("setConfig".equals(request.getParameter("action")))
	{
		String param = request.getParameter("param");
		String value = request.getParameter("value");
		if("forumName".equals(param))
		{
			ForumAPI.Config.setForumName(value);
			response.sendRedirect(request.getRequestURI());
			ForumAPI.closeDB();
			return;
		}
	}
	if("fillDataBase".equals(request.getParameter("action")))
	{
		if(!ForumAPI.loggedUserAsAdmin())
			return;
		
		DBUser dbuser = ForumAPI.userLogin();
		
		ForumAPI.Config.setForumName("Forum de debug");
		
		
		for(int icat=0; icat<5; icat++)
		{
			DBCategory cat = ForumAPI.createCategory("Categorie " + icat, "Catégorie de test "+icat);
			for(int itop=0; itop<5; itop++)
			{
				DBTopic top = ForumAPI.createTopic(cat, "Topic " + itop, "Topic " + itop + " dans la catégorie " + icat);
				for(int iconv=0; iconv<5; iconv++)
				{
					DBConversation conv = ForumAPI.createConversation(top, "Conversation " + iconv, "Conversation de test " + iconv);
					for(int icom=0; icom<5; icom++)
					{
						ForumAPI.createComment(conv, "Message " + icom + " dans le topic " + itop + " de la catégorie " + icat, dbuser);
					}
				}
			}
		}
		
		response.sendRedirect(request.getRequestURI());
		ForumAPI.closeDB();
		return;
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<H1>Bienvenue sur <%=ForumAPI.Config.getForumName() %></H1>
<H1>Utilisateur</H1>
<%
	DBUser dbuser = ForumAPI.userLogin();
	if(dbuser == null)
	{
		%><p>Utilisateur non loggé</p>
		<a href=<%=ForumAPI.userLoginLogoutUrl(request.getRequestURI()) %>>Se logger</a><%
	}
	else
	{
		%><p>Utilisateur loggé. Bienvenue <%=dbuser.getEmail() %>
		<a href=<%=ForumAPI.userLoginLogoutUrl(request.getRequestURI()) %>>Se déconnecter</a>
		<% 
		if(dbuser.isFirstLogin())
		{
			%><p>Please accept our condition <a href="testing.jsp?action=acceptCon">Accept</a></p><%
		}
	}
%>
<H1>Categories</H1>
<%
	List<DBCategory> cats = ForumAPI.getAllCategory();
	for(DBCategory c: cats)
	{
		 %><p><a href='testing.jsp?cat=<%= c.getKeyStr() %>'><%= c.getTitre() %></a> : <%= c.getDescription() %></p><%
	}
%>
<form action="testing.jsp">
	<input type="hidden" name="action" value="createCat" />
	<p>Name : <input name="name" /></p>
	<p>Descr : <input name="descr" /></p>
	<input type="submit" />
</form>
<%
if(request.getParameter("cat") != null)
{
	DBCategory dbcategory = ForumAPI.getCategory(request.getParameter("cat"));
	if(dbcategory == null)
	{
		%><p>ERROR In category</p><%
		return;
	}
	%>
	<H1>Category <%= dbcategory.getTitre() %></H1>
	<%
	List<DBTopic> topics = ForumAPI.getAllTopicOnCategory(dbcategory);
	for(DBTopic t: topics)
	{
		%><p><a href='testing.jsp?cat=<%= dbcategory.getKeyStr() %>&top=<%=t.getKeyStr() %>'><%= t.getTitle() %></a> : <%= t.getDescription() %></p><%
	}
	%>
	<form action="testing.jsp">
	<input type="hidden" name="action" value="createTopic" />
	<input type="hidden" name="cat" value="<%=dbcategory.getKeyStr() %>" />
	<p>Name : <input name="name" /></p>
	<p>Descr : <input name="descr" /></p>
	<input type="submit" />
	</form>
	<% 
}

if(request.getParameter("top") != null)
{
	DBTopic dbtopic = ForumAPI.getTopic(request.getParameter("top"));
	if(dbtopic == null)
	{
		%><p>ERROR In topic</p><%
		return;
	}
	%>
	<H1>Topic <%= dbtopic.getTitle() %></H1>
	
	<form action="testing.jsp">
	<input type="hidden" name="top" value="<%=dbtopic.getKeyStr() %>" />
	<p>From : <input name="start"/>	<br/><br/>To : <input name="end"/> </p>
	<input type="submit" />
	</form>
	
	<%
	List<DBConversation> convers = null;
	if(request.getParameter("start") != null && request.getParameter("end") != null)
	{
		long firstNum = Long.parseLong(request.getParameter("start"));
		long endNum = Long.parseLong(request.getParameter("end"));
		convers= ForumAPI.getConversationsWithTopic(dbtopic,firstNum, endNum);
	}
	else
	{
		convers = ForumAPI.getAllConversationWithTopic(dbtopic);
	}
	for(DBConversation c: convers)
	{
		%><p><a href='testing.jsp?top=<%=dbtopic.getKeyStr() %>&conv=<%=c.getKeyStr() %>'><%= c.getTitle() %></a> : <%=c.getDateLastComment().toGMTString()%></p><%
	}
	%>
	<form action="/forumAction" method="post">
	<input type="hidden" name="action" value="addConversation" />
	<input type="hidden" name="topicKey" value="<%=dbtopic.getKeyStr() %>" />
	<p>Title : <input name="titleConversation" /></p>
	<p>Descr : <input name="descriptionConversation" /></p>
	<input type="submit" />
	</form>
	<%
}

if(request.getParameter("conv") != null)
{
	DBConversation dbconversation = ForumAPI.getConversation(request.getParameter("conv"));
	if(dbconversation == null)
	{
		%><p>ERROR In Conversation</p><%
		return;
		}
		
		List<DBComment> comments = null;
		if(request.getParameter("start") != null && request.getParameter("end") != null)
		{
			long firstNum = Long.parseLong(request.getParameter("start"));
			long endNum = Long.parseLong(request.getParameter("end"));
			comments = ForumAPI.getCommentsOfConversation(dbconversation,firstNum, endNum);
		}
		else
		{
			comments = ForumAPI.getAllCommentsOfConversation(dbconversation);
		}
	%>
	<H1>Conversation <%= dbconversation.getTitle() %></H1>
	<%
	for(DBComment c: comments)
	{
		%><p><b><%=c.getAuthor().getEmail() %></b><br/><%=c.getTextComment().replaceAll("\n", "<br />")%> <%
	}
	%>
	<form action="testing.jsp">
	<input type="hidden" name="conv" value="<%=dbconversation.getKeyStr() %>" />
	<p>From : <input name="start"/>	<br/><br/>To : <input name="end"/> </p>
	<input type="submit" />
	</form>
	
	<form action="/forumAction" method="post">
	<input type="hidden" name="action" value="addComment" />
	<input type="hidden" name="conversationKey" value="<%=dbconversation.getKeyStr() %>" />
	<p>Message : <textarea name="comment"></textarea></p>
	<input type="submit" />
	</form>
	<%
}

%>
</body>
</html>
<% ForumAPI.closeDB(); %>
